<?php
namespace app\index\controller;

use think\Controller;
use think\Cookie;
use think\Db;
use think\Request;

class Sign extends Controller
{
    public function verifyId($userId)
    {
        $res = Db::table('user')->where('Number', $userId)->find();
        if (is_null($res)) {
            $arr = array(
                'succ' => false,
                'state' => 0,
                'data' => '',
            );
        } else {
            $arr = array(
                'succ' => true,
                'state' => 1,
                'data' => '',
            );
        }
        $res = json_encode($arr);
        return $res;
    }

    public function submit($userId, $password)
    {

        $res = Db::table('user')->where('Number', $userId)->value('password');
        if ($res == $password) {
            $arr = array(
                'succ' => true,
                'state' => 1,
                'data' => '',
            );
            Cookie::set('number', $userId, time() + 3600);
            //           Cookie::set('password',$password,time()+3600);
        } else {
            $arr = array(
                'succ' => false,
                'state' => 0,
                'data' => '',
            );
        }
        $res = json_encode($arr);
        return $res;
    }

    public function updataImg(Request $request)
    {
        $file = $request->file('file');
        if (empty($file)) {
            $arr = array(
                'succ' => false,
                'state' => 0,
                'data' => '',
            );
        }
        // 移动到框架应用根目录/public/uploads/ 目录下
        //$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        $info = $file->move(ROOT_PATH . 'public/static/picture/userHeadImg/'.$request->cookie('number'),'img.png');
        if ($info) {
            $arr = array(
                'succ' => true,
                'state' => 1,
                'data' =>$request->cookie('number'),
            );
        } else {
            // 上传失败获取错误信息
            $arr = array(
                'succ' => false,
                'state' => 0,
                'data' => $file->getError(),
            );
        }
        $res = json_encode($arr);
        return $res;

    }
    public function sign(){
        return $this->fetch('sign');
    }
    public function loginindex(){
        return $this->fetch('loginindex');
    }
}
